﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.Design;
using System.Reflection;

namespace ODataSource
{
    public class ODataSourceFieldSchema : IDataSourceFieldSchema
    {
        private string _name;
        private Type _datatype;
        private bool _identity, _isreadonly, _isunique, _nullable, _pk;
        int _length, _precision, _scale;
        private PropertyInfo propertyInfo;

        public ODataSourceFieldSchema(string Name, Type DataType, bool Identity,
            bool IsReadOnly, bool IsUnique, int Length, bool Nullable, bool PrimaryKey,
            int Precision, int Scale)
        {
            _name = Name;
            _datatype = DataType;
            _identity = Identity;
            _isreadonly = IsReadOnly;
            _isunique = IsUnique;
            _length = Length;
            _nullable = Nullable;
            _pk = PrimaryKey;
            _precision = Precision;
            _scale = Scale;
        }

        public ODataSourceFieldSchema(PropertyInfo propertyInfo)
        {
            this.propertyInfo = propertyInfo;
            _name = propertyInfo.Name;
            _datatype = propertyInfo.PropertyType;
            _identity = false;
            _isreadonly = !propertyInfo.CanWrite;
            _isunique = false;
            _length = int.MaxValue;
            _nullable = true;
            _pk = false;
            _precision = -1;
            _scale = -1;
        }

        // Name is ID
        public string Name
        {
            get { return _name; }
        }
        // Data type is string
        public Type DataType
        {
            get { return _datatype; }
        }
        // This is not an Identity field
        public bool Identity
        {
            get { return _identity; }
        }
        // This field is read only
        public bool IsReadOnly
        {
            get { return _isreadonly; }
        }
        // This field is unique
        public bool IsUnique
        {
            get { return _isunique; }
        }
        // This field can't be longer than 20
        public int Length
        {
            get { return _length; }
        }
        // This field can't be null
        public bool Nullable
        {
            get { return _nullable; }
        }
        // This is a Primary Key
        public bool PrimaryKey
        {
            get { return _pk; }
        }

        // These properties do not apply
        public int Precision
        {
            get { return _precision; }
        }
        public int Scale
        {
            get { return _scale; }
        }
    }
}
